/*
 * Copyright (c) 2025 HiEasyX
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/**
 * \file hex_checkbox.cpp
 * \brief The checkbox for HiEasyX
 */

#include <include/hex_checkbox.h>
#include <include/hex.h>

namespace HX {
bool Checkbox(const HXString &Title, CheckboxProfile &Profile) {
	if (RoughVisibility()) {
		return false;
	}

	auto &context = GetContext();
	auto &theme   = GetTheme();

	auto position = CurrentLayoutPosition();

	auto checkbox = HXRect{position.X, position.Y, position.X + 22, position.Y + 22};

	auto painter = CurrentPainter();

	auto textRectangle = painter->MeasureText(Title, HXFont{}, 18);
	auto textPosition  = HXPoint{position.X + 22 + 5, position.Y + 3};

	auto availableRectangle = HXRect{position.X, position.Y, position.X + 22 + 5 + textRectangle.Right,
	                                 position.Y + 22};

	if (AccVisibility({availableRectangle.CalWidth(), availableRectangle.CalHeight()})) {
		return false;
	}

	// Process the presse or hover
	for (auto &message : context.MessageQuery) {
		if (message.Processed) {
			continue;
		}

		auto mouse = ClipCoord({message.MouseX, message.MouseY});

		if (message.MouseLeftRelease && availableRectangle.PointInside(mouse)) {
			Profile.Checked = !Profile.Checked;
		}
	}

	float checkIconVertexes[3][2] = {
		{0.2f, 0.8f},
		{0.3, 1.f},
		{0.8f, 0.4f}
	};

	painter->Begin();

	painter->DrawFilledRectangle(checkbox, theme.CheckboxBackground, theme.CheckboxBackground);
	painter->DrawText(Title, HXFont{}, textPosition, theme.CheckboxText, 18);

	if (Profile.Checked) {
		HXPoint checkIcon[3];
		for (int i = 0; i < 3; i++) {
			checkIcon[i] = {static_cast<HXGInt>(checkIconVertexes[i][0] * 16 + position.X + 6),
			                static_cast<HXGInt>(checkIconVertexes[i][1] * 16 + position.Y)};
		}

		HXLineStyle style;
		style.Thickness = 2;

		painter->DrawLineStyled(checkIcon[0], checkIcon[1], theme.CheckboxActiveCross, style);
		painter->DrawLineStyled(checkIcon[1], checkIcon[2], theme.CheckboxActiveCross, style);
	}

	painter->End();

	LayoutNextControl({ availableRectangle.CalWidth(), availableRectangle.CalHeight() });

	return Profile.Checked;
}
}
